import 'package:base/navigator/navigator_controller.dart';
import 'package:flutter/material.dart';
import 'package:base/vm/view_model_state.dart';
import 'package:base/vm/view_model.dart';
import 'package:pipeline/model/bean/login_entity.dart';
import 'package:pipeline/view/dialog/progress_dialog.dart';
import 'package:pipeline/view/pages/home_page.dart';

///ChangeNotifierProvider.value(value: LoginViewModel())
class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends ViewModelState<LoginPage, LoginViewModel> {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(model._entity?.name ?? "没登陆"),
            TextButton(
              child: Text("登录"),
              onPressed: () async {
                ///dialog
                ///
                ProgressDialog _dialog = ProgressDialog();
                NavigatorController.showDialog(
                  context,
                  _dialog,
                );
                await fetchModel.login(userName: "david", password: "123456");
                //hide dialog;
                NavigatorController.hideDialog(context, _dialog);

                NavigatorController.startPage(context, HomePage(),
                    isReplaceCurrent: true);
              },
            ),
          ],
        ),
      ),
    );
  }
}

class LoginViewModel extends ViewModel {
  LoginEntity? _entity;

  Future<void> login({String? userName, String? password}) async {
    await Future.delayed(Duration(milliseconds: 500), () {
      _entity = LoginEntity(name: "david");
      notifyListeners();
    });
  }
}
